import axios from "axios";

const httpService = axios.create({
    baseURL:'http://8.155.160.117/api',
    // baseURL:'http://localhost:9090',
    timeout: 8000
})

// 请求拦截器
httpService.interceptors.request.use(function (config) {
    config.headers.Authorization = window.sessionStorage.getItem("token");
    return config;
}, function (error) {
    return Promise.reject(error);
})

// 响应拦截器
httpService.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    return Promise.reject(error);
})

// get请求
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'get',
            params: params
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error)
        })
    })
}

// post请求
export function post(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            data: params
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error)
        })
    })
}

// postJson请求
export function postJson(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            headers: {'Content-Type':'application/json'},
            data: params
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error)
        })
    })
}

// post请求
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: {'Content-Type': 'multipart/form-data'}
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error)
        })
    })
}

export default {
    get,
    post,
    postJson,
    fileUpload
}
